// pages/addguanggaoshezhi/addguanggaoshezhi.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectLei: false, //项目选项
    selectHuo: false, //项目选项
    tihuoWay: '商品', //连接类型默认选项
    tihuoDong: '团拼活动', //连接活动默认选项
    lianjieGoods: '请选择商品',
    goodsInfo: '', //选择的商品
    guanggaoId: '',
    duodongXuhao: '',
    gongGao: '',
    imgURL: '',
  },
  // 获取输入的公告
  getgonggao(e) {
    // console.log(e.detail.value)
    this.setData({
      gongGao: e.detail.value
    })
  },
  getxuhao(e) {
    // console.log(e.detail.value)
    this.setData({
      duodongXuhao: e.detail.value
    })
  },
  // 添加图片
  changeChat() {
    let that = this
    wx.navigateTo({
      url: '/pages/setting/page/cropperlunbo/cropperlunbo',
    })
  },
  //去连接搜索商品界面
  tolianjie() {
    wx.navigateTo({
      url: '/pages/setting/page/sousuogoods/sousuogoods',
    })
  },
  // 连接类型
  leiShowMsg() {
    if (!this.data.selectLei) {
      this.setData({
        selectHuo: false
      })
    }
    this.setData({
      selectLei: !this.data.selectLei
    })
  },
  // 类型选择下拉值
  leiSelect(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      tihuoWay: name,
      selectLei: false
    })
  },
  // 活动
  huoShowMsg() {
    if (!this.data.selectHuo) {
      this.setData({
        selectLei: false
      })
    }
    this.setData({
      selectHuo: !this.data.selectHuo
    })
  },
  // 活动选择下拉值
  huoSelect(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      tihuoDong: name,
      selectHuo: false
    })
  },
  tijiao(){
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定提交该广告信息吗？',
      success (res) {
        if (res.confirm) {
          that.addguanggaoS()
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
      
     
  },
  // 完成添加
  addguanggaoS() {
    let that = this
    let types, urlType
    
    if (that.data.guanggaoId) {
      types = 1
    } else {
      types = 0
    }
    if (!that.data.duodongXuhao) {
      setTimeout(function () {
        wx.showToast({
          title: '请输入序号',
          icon: 'none',
          duration: 1500
        })
      }, 0)
      return
    }
    if (!that.data.imgURL) {
      setTimeout(function () {
        wx.showToast({
          title: '请选择图片',
          icon: 'none',
          duration: 1500
        })
      }, 0)
      return
    }
    if (that.data.tihuoWay == '活动公告') {
      if (!that.data.gongGao) {
        setTimeout(function () {
          wx.showToast({
            title: '请填写公告',
            icon: 'none',
            duration: 1500
          })
        }, 0)
        return
      }
      wx.request({
        url: app.globalData.myurl,
        data: {
          cmd: "upShopImg",
          shopId: wx.getStorageSync('shopId'),
          imgId: that.data.guanggaoId,
          seq: that.data.duodongXuhao,
          type: types,
          urlType: 2,
          urlValue: that.data.gongGao,
          imgUrl: that.data.imgURL
        },
        header: {
          "content-type": "application/x-www-form-urlencoded",
          "token": wx.getStorageSync('token')
        },
        success(res) {
          if (res.data.zjstate == 1) {
            setTimeout(function () {
              wx.hideLoading()
              wx.stopPullDownRefresh() //停止下拉刷新
            }, 0)
            setTimeout(function () {
              wx.showModal({
                title: '您的账号已在其他设备登录，请重新登录！',
                showCancel: false, //是否显示取消按钮false去掉取消按钮
                success: function (res) {
                  if (res.confirm) {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  } else {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  }
                }
              })
            }, 0)
            return
          }
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
          console.log(res)
          if (res.data.result == 0) {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }
        }
      });
    } else if (that.data.tihuoWay == '商品') {
      if (!that.data.goodsInfo) {
        setTimeout(function () {
          wx.showToast({
            title: '请选择商品',
            icon: 'none',
            duration: 1500
          })
        }, 0)
        return
      }
      wx.request({
        url: app.globalData.myurl,
        data: {
          cmd: "upShopImg",
          shopId: wx.getStorageSync('shopId'),
          imgId: that.data.guanggaoId,
          seq: that.data.duodongXuhao,
          type: types,
          urlType: 0,
          urlValue: (that.data.goodsInfo.urlvalue || that.data.goodsInfo.goodsId),
          imgUrl: that.data.imgURL
        },
        header: {
          "content-type": "application/x-www-form-urlencoded",
          "token": wx.getStorageSync('token')
        },
        success(res) {
          if (res.data.zjstate == 1) {
            setTimeout(function () {
              wx.hideLoading()
              wx.stopPullDownRefresh() //停止下拉刷新
            }, 0)
            setTimeout(function () {
              wx.showModal({
                title: '您的账号已在其他设备登录，请重新登录！',
                showCancel: false, //是否显示取消按钮false去掉取消按钮
                success: function (res) {
                  if (res.confirm) {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  } else {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  }
                }
              })
            }, 0)
            return
          }
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
          console.log(res)
          if (res.data.result == 0) {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }
        }
      });
    } else if (that.data.tihuoWay == '活动') {
      let vale
      if (that.data.tihuoDong == '团拼活动') {
        vale = 1
      } else if (that.data.tihuoDong == '满减活动') {
        vale = 3
      } else if (that.data.tihuoDong == '限时抢购') {
        vale = 4
      } else if (that.data.tihuoDong == '满送活动') {
        vale = 2
      }
      wx.request({
        url: app.globalData.myurl,
        data: {
          cmd: "upShopImg",
          shopId: wx.getStorageSync('shopId'),
          imgId: that.data.guanggaoId,
          seq: that.data.duodongXuhao,
          type: types,
          urlType: 1,
          urlValue: vale,
          imgUrl: that.data.imgURL
        },
        header: {
          "content-type": "application/x-www-form-urlencoded",
          "token": wx.getStorageSync('token')
        },
        success(res) {
          if (res.data.zjstate == 1) {
            setTimeout(function () {
              wx.hideLoading()
              wx.stopPullDownRefresh() //停止下拉刷新
            }, 0)
            setTimeout(function () {
              wx.showModal({
                title: '您的账号已在其他设备登录，请重新登录！',
                showCancel: false, //是否显示取消按钮false去掉取消按钮
                success: function (res) {
                  if (res.confirm) {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  } else {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  }
                }
              })
            }, 0)
            return
          }
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
          console.log(res)
          if (res.data.result == 0) {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    if (options.obj) {
      let obj = JSON.parse(options.obj);
      console.log(obj)
      if (obj.urltype == 1) {
        let val
        if (obj.urlvalue == 1) {
          val = '团拼活动'
        } else if (obj.urlvalue == 2) {
          val = '满送活动'
        } else if (obj.urlvalue == 3) {
          val = '满减活动'
        } else if (obj.urlvalue == 4) {
          val = '限时抢购'
        }
        that.setData({
          tihuoWay: '活动',
          duodongXuhao: obj.seq,
          imgURL: obj.img,
          guanggaoId: obj.imgId,
          tihuoDong: val
        })

      } else if (obj.urltype == 2) {
        that.setData({
          tihuoWay: '活动公告',
          duodongXuhao: obj.seq,
          imgURL: obj.img,
          guanggaoId: obj.imgId,
          gongGao: obj.urlvalue,
        })
      } else if (obj.urltype == 0) {
        that.setData({
          goodsInfo: obj,
          tihuoWay: '商品',
          duodongXuhao: obj.seq,
          imgURL: obj.img,
          guanggaoId: obj.imgId,
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '众商联采',
      path: "/pages/login/login", // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: app.globalData.logo
    }
  }
})